package com.fastwire.carriere.hibernate.daoimpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.ProtectionGroupDao;
import com.fastwire.carriere.inventory.entity.ProtectionGroup;

// TODO: Auto-generated Javadoc
/**
 * The Class ProtectionGroupDaoImpl.
 */
public class ProtectionGroupDaoImpl extends AbstractDAOImpl implements
		ProtectionGroupDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.ProtectionGroupDao#saveOrUpdatePG(com.fastwire
	 * .carriere.inventory.entity.ProtectionGroup)
	 */
	@Override
	public void saveOrUpdatePG(ProtectionGroup protectionGroup)
			throws DAOException {
		super.saveOrUpdate(protectionGroup);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.ProtectionGroupDao#findProtectionGroupbyNEName
	 * (java.lang.String)
	 */
	@Override
	public List findProtectionGroupbyNEName(String neID) throws DAOException {
		Session session = HibernateUtil.openSession();
		try {
			Criteria criteria = session.createCriteria(ProtectionGroup.class);
			criteria = criteria.add(getStatusCriteria());
			criteria.add(Restrictions.like("name", neID, MatchMode.ANYWHERE));

			return criteria.list();
		} catch (Exception ex) {
			if (session.getTransaction().isActive()) {
				session.getTransaction().rollback();
			}
			throw new DAOException(ex);
		} finally {
			HibernateUtil.close(session);

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.ProtectionGroupDao#findProtectionGroupbyMemberName
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public ProtectionGroup findProtectionGroupbyMemberName(String tunnel,
			String neID) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(ProtectionGroup.class);
		criteria = criteria.add(getStatusCriteria());
		Criterion criterion = Restrictions.like("working", tunnel,
				MatchMode.ANYWHERE);
		criterion = Restrictions
				.or(criterion, Restrictions.like("workingReverse", tunnel,
						MatchMode.ANYWHERE));

		criterion = Restrictions.or(criterion,
				Restrictions.like("protection", tunnel, MatchMode.ANYWHERE));
		criterion = Restrictions.or(criterion, Restrictions.like(
				"protectionReverse", tunnel, MatchMode.ANYWHERE));

		criteria.add(criterion);
		return (ProtectionGroup) getHibernateTemplate()
				.findSingleObjByCriteria(criteria, false, false);

	}

	@Override
	public void invalidateAll() throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ProtectionGroup.class);
		criteria = criteria.add(getStatusCriteria());
		getHibernateTemplate().invalidateObjsByCriteria(criteria);

	}

	public List<ProtectionGroup> findAllProtectionGroups() throws DAOException {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(ProtectionGroup.class);

		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

}
